/**
 * 
 */
package com.hospital.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.hospital.dao.IKeshiDao;
import com.hospital.vo.Keshi;

/**
 * @author Administrator
 *
 */
public class KeshiDaoImpl implements IKeshiDao {
	private Connection conn = null;
	private PreparedStatement pstmt = null;
	private ResultSet rs = null;
	public KeshiDaoImpl(Connection conn) {
		this.conn = conn;
	}
	/* (non-Javadoc)
	 * @see com.hospital.dao.IKeshiDao#doCreate(com.hospital.vo.Keshi)
	 */
	@Override
	public boolean doCreate(Keshi keshi) throws Exception {
		boolean flag = false;
		try {
			String sql = "INSERT INTO keshi(kname) VALUES (?)";
			this.pstmt = this.conn.prepareStatement(sql);
			this.pstmt.setString(1, keshi.getKname());
			if(this.pstmt.executeUpdate() > 0) {
				flag = true;
			}
		} catch (Exception e) {
			throw e;
		} finally {
			if(this.pstmt != null) {
				try {
					this.pstmt.close();
				} catch (Exception e) {
					throw e;
				}
			}
		}
		return flag;
	}

	@Override
	public boolean doUpdate(Keshi keshi) throws Exception {
		boolean flag = false;
		try {
			String sql = "UPDATE keshi SET kname=? WHERE kid=?";
			this.pstmt = this.conn.prepareStatement(sql);
			this.pstmt.setString(1, keshi.getKname());
			this.pstmt.setInt(2, keshi.getKid());
			if(this.pstmt.executeUpdate() > 0) {
				flag = true;
			}
		} catch (Exception e) {
			throw e;
		} finally {
			if(this.pstmt != null) {
				try {
					this.pstmt.close();
				} catch (Exception e) {
					throw e;
				}
			}
		}
		return flag;
	}
	@Override
	public boolean doDel(int kid) throws Exception {
		boolean flag = false;
		try {
			String sql = "DELETE FROM keshi WHERE kid=?";
			this.pstmt = this.conn.prepareStatement(sql);
			this.pstmt.setInt(1, kid);
			if(this.pstmt.executeUpdate() > 0) {
				flag = true;
			}
		} catch (Exception e) {
			throw e;
		} finally {
			if(this.pstmt != null) {
				try {
					this.pstmt.close();
				} catch (Exception e) {
					throw e;
				}
			}
		}
		return flag;
	}
	@Override
	public List<Keshi> findAll() throws Exception {
		List<Keshi> list = null;
		try {
			String sql = "SELECT * FROM keshi";
			this.pstmt = this.conn.prepareStatement(sql);
			this.rs = this.pstmt.executeQuery();
			list = new ArrayList<Keshi>();
			while(this.rs.next()) {
				Keshi keshi = new Keshi();
				keshi.setKid(this.rs.getInt("kid"));
				keshi.setKname(this.rs.getString("kname"));
				list.add(keshi);
			}
		} catch (Exception e) {
			throw e;
		} finally {
			if(this.rs != null) {
				try {
					this.rs.close();
				} catch (Exception e) {
					throw e;
				}
			}
			if(this.pstmt != null) {
				try {
					this.pstmt.close();
				} catch (Exception e) {
					throw e;
				}
			}
		}
		return list;
	}
	@Override
	public Keshi findById(int kid) throws Exception {
		Keshi keshi = null;
		try {
			String sql = "SELECT * FROM keshi WHERE kid=?";
			this.pstmt = this.conn.prepareStatement(sql);
			this.pstmt.setInt(1, kid);
			this.rs = this.pstmt.executeQuery();
			if(this.rs.next()) {
				keshi = new Keshi();
				keshi.setKid(this.rs.getInt("kid"));
				keshi.setKname(this.rs.getString("kname"));
			}
		} catch (Exception e) {
			throw e;
		} finally {
			if(this.rs != null) {
				try {
					this.rs.close();
				} catch (Exception e) {
					throw e;
				}
			}
			if(this.pstmt != null) {
				try {
					this.pstmt.close();
				} catch (Exception e) {
					throw e;
				}
			}
		}
		return keshi;
	}

}
